/*
 * @(#)Launcher.java        0.1 01-Mar-2009
 *
 * Copyright 2008 Thomas Ford. All rights reserved.
 */


package edu.napier.soc.xfdm.launcher;

import edu.napier.soc.xfdm.uci.MainGUI;
import edu.napier.soc.xfdm.exceptions.ExceptionHandler;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 * 
 * @version 0.1, 01-Mar-2009
 * @author  Thomas Ford
 */
public class Launcher {

	private static MainGUI gui;
	private static boolean SUPPRESS_ERRORS = false;
	private static boolean DEFAULT_LAF = false;
	private static File OPEN_DB_FILE;
	private static File OPEN_TXT_FILE;
	private static String HELP_TEXT;
	
    public static void main(String[] args) {
		parseArgs(args);
		
		if (!DEFAULT_LAF) {
			try {
				UIManager.setLookAndFeel(
						UIManager.getSystemLookAndFeelClassName());
			} catch (Exception ex) {
				System.err.println(ex.getMessage());
		   }
		}

		if (!SUPPRESS_ERRORS) {
			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
		}

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                gui = new MainGUI();

				if (OPEN_DB_FILE != null) {
					gui.loadDatabase(OPEN_DB_FILE);
				}
				if (OPEN_TXT_FILE != null) {
					gui.loadStatements(OPEN_TXT_FILE);
				}
				gui.setVisible(true);

				if (HELP_TEXT != null) {
					gui.showConsoleCommands(HELP_TEXT);
				}
            }
        });
    }

	private static void parseArgs(String[] args) {
		Options o = new Options();
		o.addOption("h", "help", false, "display help");
		o.addOption("l", "defaultlaf", false,
				"use the default look and feel (faster GUI)");
		o.addOption("o", "opendb", true, "open a database from file");
		o.addOption("s", "suppress", false, "suppress unchecked exception window");
		o.addOption("t", "opentxt", true,
				"open a text document containing statements");

		CommandLineParser parser = new PosixParser();
		
		try {
			CommandLine cli = parser.parse(o, args, false);
			if (cli.hasOption('h')) {
				HelpFormatter hf = new HelpFormatter();
				StringWriter sw = new StringWriter();
				PrintWriter pw = new PrintWriter(sw);
				hf.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, "XFDM", null, o,
						HelpFormatter.DEFAULT_LEFT_PAD,
						HelpFormatter.DEFAULT_DESC_PAD, null, true);
				HELP_TEXT = sw.getBuffer().toString();
			}
			if (cli.hasOption('o')) {
				OPEN_DB_FILE = new File(cli.getOptionValue('o'));
			}
			if (cli.hasOption('s')) {
				SUPPRESS_ERRORS = true;
			}
			if (cli.hasOption('l')) {
				DEFAULT_LAF = true;
			}
			if (cli.hasOption('t')) {
				OPEN_TXT_FILE = new File(cli.getOptionValue('t'));
			}

		} catch (ParseException ex) {
			HELP_TEXT = ex.getMessage() + "\n" +
					"Use -h for command-line help";
		}
	}
}